package com.tucc.hello.leetcode.no1_400.no1_200.no61_80.no75;

import java.util.Random;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        Random random = new Random();
        int len = random.nextInt(20);
        int[] nums = new int[len];
        for (int i = 0; i < len; i++) {
            nums[i] = random.nextInt(3);
            System.out.print(nums[i] + " ");
        }
        System.out.println();
        solution.sortColors(nums);
        for (int i = 0; i < len; i++) {
            System.out.print(nums[i] + " ");
        }
        System.out.println();
    }

    public void sortColors(int[] nums) {
        int head = 0;
        int tail = nums.length - 1;
        for (int i = 0; i <= tail; i++) {
            if (nums[i] == 0) {
                swap(nums, head, i);
                head++;
            }
            if (nums[i] == 2) {
                swap(nums, tail, i);
                tail--;
                i--;
            }
        }
    }

    public void swap(int[] nums, int x, int y) {
        if (x != y) {
            int temp = nums[x];
            nums[x] = nums[y];
            nums[y] = temp;
        }
    }

}
